﻿@model DevExtreme.NETCore.Demos.Models.FileManagement.AzureStorageAccount

@if(!Model.IsEmpty()) {
    @(Html.DevExtreme().FileManager()
        .ID("file-manager")
        .FileSystemProvider(provider => provider.Custom()
            .GetItems("getItems")
            .CreateDirectory("createDirectory")
            .RenameItem("renameItem")
            .DeleteItem("deleteItem")
            .CopyItem("copyItem")
            .MoveItem("moveItem")
            .UploadFileChunk("uploadFileChunk")
            .DownloadItems("downloadItems")
        )
        .Permissions(permissions => {
            permissions.Download(true);
            // uncomment the code below to enable file/folder management
            // permissions.Create(true);
            // permissions.Copy(true);
            // permissions.Move(true);
            // permissions.Delete(true);
            // permissions.Rename(true);
            // permissions.Upload(true);
        })
        .Upload(upload => upload.MaxFileSize(1048576))
        .AllowedFileExtensions(new string[0])
    )
    <text>
        <div id="request-panel"></div>
        <script src="~/utils/azure-file-system.js"></script>
        <script>
            function getItems(parentDirectory) {
                return azure.getItems(parentDirectory.path);
            }

            function createDirectory(parentDirectory, name) {
                return azure.createDirectory(parentDirectory.path, name);
            }

            function renameItem(item, name) {
                return item.isDirectory ? azure.renameDirectory(item.path, name) : azure.renameFile(item.path, name);
            }

            function deleteItem(item) {
                return item.isDirectory ? azure.deleteDirectory(item.path) : azure.deleteFile(item.path);
            }

            function copyItem(item, destinationDirectory) {
                var destinationPath = destinationDirectory.path ? destinationDirectory.path + "/" + item.name : item.name;
                return item.isDirectory ? azure.copyDirectory(item.path, destinationPath) : azure.copyFile(item.path, destinationPath);
            }

            function moveItem(item, destinationDirectory) {
                var destinationPath = destinationDirectory.path ? destinationDirectory.path + "/" + item.name : item.name;
                return item.isDirectory ? azure.moveDirectory(item.path, destinationPath) : azure.moveFile(item.path, destinationPath);
            }

            function uploadFileChunk(fileData, uploadInfo, destinationDirectory) {
                var deferred = null;

                if(uploadInfo.chunkIndex === 0) {
                    var filePath = destinationDirectory.path ? destinationDirectory.path + "/" + fileData.name : fileData.name;
                    deferred = gateway.getUploadAccessUrl(filePath).done(function(accessUrl) {
                        uploadInfo.customData.accessUrl = accessUrl;
                    });
                } else {
                    deferred = $.Deferred().resolve().promise();
                }

                deferred = deferred.then(function() {
                    return gateway.putBlock(uploadInfo.customData.accessUrl, uploadInfo.chunkIndex, uploadInfo.chunkBlob);
                });

                if(uploadInfo.chunkIndex === uploadInfo.chunkCount - 1) {
                    deferred = deferred.then(function() {
                        return gateway.putBlockList(uploadInfo.customData.accessUrl, uploadInfo.chunkCount);
                    });
                }

                return deferred.promise();
            }

            function downloadItems(items) {
                azure.downloadFile(items[0].path);
            }

            function onRequestExecuted(e) {
                $("<div>").addClass("request-info").append(
                        createParameterInfoDiv("Method:", e.method),
                        createParameterInfoDiv("Url path:", e.urlPath),
                        createParameterInfoDiv("Query string:", e.queryString),
                        $("<br>")
                    )
                    .prependTo("#request-panel");
            }

            function createParameterInfoDiv(name, value) {
                return $("<div>").addClass("parameter-info").append(
                    $("<div>").addClass("parameter-name").text(name),
                    $("<div>").addClass("parameter-value dx-theme-accent-as-text-color").text(value).attr("title", value)
                );
            }

            var endpointUrl = '@Url.RouteUrl("FileManagerAzureAccessApi")';
            var gateway = new AzureGateway(endpointUrl, onRequestExecuted);
            var azure = new AzureFileSystem(gateway);
        </script>
    </text>
}
else {
    <text>
        To run the demo locally, specify your Azure storage account name, access key and container name in the appsettings.json file.
        Refer to the <a href="https://demos.devexpress.com/ASPNetCore/Demo/FileManager/AzureClientBinding/" target="_blank">
            https://demos.devexpress.com/ASPNetCore/Demo/FileManager/AzureClientBinding/
        </a> to see the demo online.
    </text>
}
